package com.yonyou.lxp.account.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yonyou.lxp.account.R;
import com.yonyou.lxp.account.entity.ExpenditureInfo;
import com.yonyou.lxp.account.utils.Constants;
import com.yonyou.lxp.account.views.MyTextView;

import java.util.Date;
import java.util.List;

/**
 * Created by liuxiaopeng on 16/2/28.
 */
public class MyAdapter extends BaseAdapter {
    private List<ExpenditureInfo> list;
    private LayoutInflater inflater;
    private Activity context;

    public MyAdapter(List list, LayoutInflater inflater,Activity context){
        this.list=list;
        this.context=context;
        this.inflater=inflater;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public ExpenditureInfo getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHelder viewHelder=null;
        if (convertView==null){
            convertView=inflater.inflate(R.layout.item_e,null);
            viewHelder=new ViewHelder();
            viewHelder.tv_item_money= (TextView) convertView.findViewById(R.id.tv_item_money);
            viewHelder.tv_item_time= (TextView) convertView.findViewById(R.id.tv_item_time);
            viewHelder.tv_item_name= (TextView) convertView.findViewById(R.id.tv_item_name);
            viewHelder.tv_item_type= (TextView) convertView.findViewById(R.id.tv_item_type);
            viewHelder.tv_item_remarks= (MyTextView) convertView.findViewById(R.id.tv_item_remarks);
            convertView.setTag(viewHelder);
        }else {
            viewHelder= (ViewHelder) convertView.getTag();
        }

        ExpenditureInfo item=list.get(position);
        if (item!=null) {
            viewHelder.tv_item_money.setText(item.getE_money());
            viewHelder.tv_item_time.setText(Constants.mFormatter.format(new Date(item.getE_time())));
            viewHelder.tv_item_name.setText(item.getT_name());
            viewHelder.tv_item_type.setText("0".equals(item.getT_type())?"-":"+");
            viewHelder.tv_item_remarks.setText(item.getE_remarks());

            if ("0".equals(item.getT_type())){
                viewHelder.tv_item_money.setTextColor(Color.RED);
                viewHelder.tv_item_time.setTextColor(Color.RED);
                viewHelder.tv_item_name.setTextColor(Color.RED);
                viewHelder.tv_item_type.setTextColor(Color.RED);
                viewHelder.tv_item_remarks.setTextColor(Color.RED);
            }else {
                viewHelder.tv_item_money.setTextColor(context.getResources().getColor(R.color.green));
                viewHelder.tv_item_time.setTextColor(context.getResources().getColor(R.color.green));
                viewHelder.tv_item_name.setTextColor(context.getResources().getColor(R.color.green));
                viewHelder.tv_item_type.setTextColor(context.getResources().getColor(R.color.green));
                viewHelder.tv_item_remarks.setTextColor(context.getResources().getColor(R.color.green));
            }
        }
        return convertView;
    }

    static class ViewHelder{
        TextView tv_item_money;
        TextView tv_item_time;
        TextView tv_item_name;
        TextView tv_item_type;
        MyTextView tv_item_remarks;
    }
}
